<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 销售商品管理
 *
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */
define('ITEM_AHEADTIME', 4);	//提前预定时间
define('ITEM_EDIBLE', 5);	//食用
define('ITEM_SIZE', 6);	//尺寸
define('ITEM_BIRTH', 7);	//生日牌
define('ITEM_SYNCO', 8);	//切分
quanli_require('module/base/goods/quanliGoods.class.php');
quanli_require('module/base/goods/commonGoods.class.php');
quanli_require('module/base/goods/goodsAdd.class.php');
quanli_require('module/base/goods/goodsBasic.class.php');
quanli_require('module/base/goods/goodsMater.class.php');
quanli_require('module/base/goods/goodsCategory.class.php');
quanli_require('module/base/goods/goodsSpec.class.php');
quanli_require('module/base/goods/goodsFittings.class.php');
quanli_require('module/base/goods/goodsNutrition.class.php');
quanli_require('module/base/goods/goodsProduce.class.php');
quanli_require('module/base/system/itemHandler.class.php');

class pa_goodsSale {

    public function execute() {
        global $G_SHOP ;
        if (isset($_REQUEST['operFlg'])) {
            switch($_REQUEST['operFlg']) {
                case 1: //销售商品信息列表
                    $this->goodsList();
                    break;
                case 2: //基本信息编辑
                    $this->goodsListSale();
                    break;
                case 3: //sku信息编辑
                    $this->goodsSkuSale();
                    break;
                case 4: //生产信息编辑
                    $this->goodsProductSale();
                    break;  
                case 5: //图片信息编辑
                    $this->goodsPicSale();
                    break;       
                default:
					backendError();
            }
        }
    }

    /**
     * 销售商品列表
     *
     */
    public function goodsList() {

        global $G_SHOP;
        
        $ary_array = array(
         	'sortname' => 'sortOrder',
         	'sortorder' => 'asc',
         	'useFlg' => 1
         );
       
        //分类
        $categoryHandler = new goodsCategory( $G_SHOP->DBCA );
        $category = $categoryHandler->searchBigCategory($ary_array);
        
         //城市
        $G_SHOP->smarty->assign_by_ref('city', get_city());
        $G_SHOP->smarty->assign_by_ref('category', $category);
        $G_SHOP->smarty->display('goods/goodsListSale.tpl');
    }

    /**
     * 基本信息编辑
     *
     */
    public function goodsListSale(){

        global $G_SHOP;
        
        $addHandler = new goodsAdd( $G_SHOP->DBCA ) ;
        
        $categoryHandler = new goodsCategory( $G_SHOP->DBCA );
         
        $materHandler = new goodsMater( $G_SHOP->DBCA );
        
        $basicHandler = new goodsBasic( $G_SHOP->DBCA );
        
        if (!isset($_REQUEST['postID'])) {
        	backendError();
        }
        
        $check_list_edit = new entity('goods_list');
        $check_list_edit->postID = $_REQUEST['postID'];
        if (!$G_SHOP->DBCA->fetch($check_list_edit)) {
        	backendError('商品信息未找到');
        }
        $check_list_edit->chineseBriefCount = 150 - (ceil(strlen($check_list_edit->chineseBrief)  / 3));
        $check_list_edit->frenchBriefCount = 300 - strlen($check_list_edit->frenchBrief);
        
        $ary_array = array(
         	'sortname' => 'sortOrder',
         	'sortorder' => 'asc',
         	'useFlg' => 1
         );
         
         $select_array = array(
         	'postID' => $_REQUEST['postID']
         );
       
        //分类
        $category = $categoryHandler->searchBigCategory( $ary_array );
        
        //商品基本信息
        $basicGroup = $basicHandler->searchBasicGroup( $ary_array );
        $basicSelec = $basicHandler->searchBasicRelation( $select_array );
        $basicSelecArray['params'] = array();
        $bSelectSort = array();
        $bMaxSort = 1;
        if ($basicSelec) {
        	foreach ($basicSelec as $s) {
        		if (!in_array($s['basicGroupID'], $basicSelecArray)) {
        			$basicSelecArray['params'][] = $s['basicGroupID'];
        			$bSelectSort[] = array(
        				'gid' => $s['basicGroupID'],
        				'sortOrder' => $s['sortOrder'],
        				'bid' => $s['basicID']
        			);
        			if ($bMaxSort < $s['sortOrder']) {
        				$bMaxSort = $s['sortOrder'];
        			}
        		}
        	}
        	$check_list_edit->bSelectSort = $bSelectSort;
        }

    	$basic_result = $addHandler->getBasicTpl( $basicSelecArray ) ;
    	if ($basic_result['code'] == 200) {
	    	$G_SHOP->smarty->assign_by_ref('basic_result', $basic_result['data']);
	    	$basicTr = $G_SHOP->smarty->fetch('goods/basicfetch.tpl');
	    	$check_list_edit->basicTr = $basicTr;
	    	$check_list_edit->bcount = $bMaxSort + 1;
    	}

        //原材料信息
        $mater = $materHandler->searchMater( $ary_array );
        $materSelec = $materHandler->searchMaterRelation( $select_array );
        $materSelecArray['params'] = array();
        $mSelectSort = array();
        $mMaxSort = 1;
        if ($materSelec) {
        	foreach ($materSelec as $m) {
        		if (!in_array($m['materID'], $materSelecArray['params'])) {
        			$materSelecArray['params'][] = $m['materID'];
        			
        			$mSelectSort[] = array(
        				'mid' => $m['materID'],
        				'sortOrder' => $m['sortOrder']
        			);
        			if ($mMaxSort < $m['sortOrder']) {
        				$mMaxSort = $m['sortOrder'];
        			}
        		}
        	}
        	$check_list_edit->mSelectSort = $mSelectSort;
        }
        
        $mater_array = $addHandler->getMaterTpl( $materSelecArray ) ;
    	if ($mater_array['code'] == 200) {
	    	$G_SHOP->smarty->assign_by_ref('mater_result', $mater_array['data']);
	    	$materTr = $G_SHOP->smarty->fetch('goods/materfetch.tpl');
	    	$check_list_edit->materTr = $materTr;
	    	$check_list_edit->mcount = $mMaxSort + 1;
    	} 
        
        //城市
        $G_SHOP->smarty->assign_by_ref('city', get_city());
        $G_SHOP->smarty->assign_by_ref('category', $category);
        $G_SHOP->smarty->assign_by_ref('basicGroup', $basicGroup);
        $G_SHOP->smarty->assign_by_ref('mater', $mater);
        
        //销售商品
        $G_SHOP->smarty->assign_by_ref('editInfo', $check_list_edit);
        
        //商品图片
        $commonHandler = new commonGoods( $G_SHOP->DBCA );
        $picture_array = array(
        	'postID' => $_REQUEST['postID'],
        	'sortname' => 'sortOrder',
        	'sortorder' => 'asc'
        );
        $picture = $commonHandler->searchGoodsPicture( $picture_array );
		$G_SHOP->smarty->assign_by_ref('picture', $picture);
		
        $G_SHOP->smarty->display('goods/goodsSale.tpl');
    }
    
    /**
     * sku信息编辑
     *
     */
    public function goodsSkuSale(){

        global $G_SHOP;
        
       	//检验参数
        if (!isset($_REQUEST['postID'])) {
        	backendError();
        }
        
        //查询二级销售商品
        $check_list_edit = new entity('goods_list');
        $check_list_edit->postID = $_REQUEST['postID'];
        if (!$G_SHOP->DBCA->fetch($check_list_edit)) {
        	backendError('商品信息未找到');
        } else {
	        //城市
	        $check_list_edit->city = get_city();
        }
        
        //请求查询参数
		$ary_array = array(
			'sortname' => 'sortOrder',
			'sortorder' => 'asc',
			'useFlg' => 1
		);
        
        //实例化分类类
        $categoryHandler = new goodsCategory( $G_SHOP->DBCA );
        $category = $categoryHandler->searchBigCategory( $ary_array );
        $check_list_edit->category = $category;
        
        //提前预定时间
        $itemHandler = new itemHandler( $G_SHOP->DBCA );
        $item_array = array(
        	'useFlg' => 1,
        	'itemCategoryID' => ITEM_AHEADTIME
        );
        $aheadTime_array = $itemHandler->searchItem( $item_array );
        $check_list_edit->aheadTime_array = $aheadTime_array;
        
        //食用
        $item_array = array(
        	'useFlg' => 1,
        	'itemCategoryID' => ITEM_EDIBLE
        );
        $edible_array = $itemHandler->searchItem( $item_array );
        $check_list_edit->edible_array = $edible_array;
        
        //尺寸
        $item_array = array(
        	'useFlg' => 1,
        	'itemCategoryID' => ITEM_SIZE
        );
        $size_array = $itemHandler->searchItem( $item_array );
        $check_list_edit->size_array = $size_array;
        
        //切分
        $item_array = array(
        	'useFlg' => 1,
        	'itemCategoryID' => ITEM_SYNCO
        );
        $syncopate_array = $itemHandler->searchItem( $item_array );
        $check_list_edit->syncopate_array = $syncopate_array;
        
        //生日牌
        $item_array = array(
        	'useFlg' => 1,
        	'itemCategoryID' => ITEM_BIRTH
        );
        $birthcard_array = $itemHandler->searchItem( $item_array );
        $check_list_edit->birthcard_array = $birthcard_array;
        
        //配件
        $fitHandler = new goodsFittings( $G_SHOP->DBCA );
        $goodsFit = $fitHandler->goodsFittings( $ary_array );
        $check_list_edit->goodsFit = $goodsFit;
        
        //三级商品
	    $specHandler = new goodsSpec( $G_SHOP->DBCA ) ;
        $quanliHandler = new quanliGoods( $G_SHOP->DBCA );
        $sale_array = array(
        	'twoPostID' => $_REQUEST['postID']
        );
        $saleGoods = $quanliHandler->searchGoodsSale( $sale_array );
        if ($saleGoods) {
        	$saleSpecID = 0;
        	foreach ($saleGoods as &$sale) {
        		$arg_array = array(
        			'postID' => $sale['postID']
        		);
        		$saleSpec_array = $specHandler->searchGoodsSpecRelation( $arg_array );
        		if ($saleSpec_array) {
        			$saleSpec_array = $saleSpec_array[0];
        			$sale['specGroupID'] = $saleSpec_array['specGroupID'];
        			$sale['specGroupName'] = $saleSpec_array['specGroupName'];
        			$sale['specID'] = $saleSpec_array['specID'];
        			$sale['specName'] = $saleSpec_array['specName'];
        			if ($saleSpecID == 0) {
        				$saleSpecID = $saleSpec_array['specGroupID'];
        			}
        		}
        	}
        	
        	$specGroup = $specHandler->goodsSpecGroupList( $ary_array );
        	$spec_array = array(
	        	'sortname' => 'sortOrder',
				'sortorder' => 'asc',
				'useFlg' => 1,
				'specGroupID' => $saleSpecID
	        );
	        $goodsSpec = $specHandler->searchGoodsSpec( $spec_array );
	        
	        $check_list_edit->selSpecGroupID = $saleSpecID;
	        $check_list_edit->specGroup = $specGroup;
        	$check_list_edit->goodsSpec = $goodsSpec;
        	$check_list_edit->saleGoods = $saleGoods;
        	
        } else {
        	//实例化规格类
	        $specGroup = $specHandler->goodsSpecGroupList( $ary_array );
	        $spec_array = array(
	        	'sortname' => 'sortOrder',
				'sortorder' => 'asc',
				'useFlg' => 1,
				'specGroupID' => $specGroup[0]['specGroupID']
	        );
	        $goodsSpec = $specHandler->searchGoodsSpec( $spec_array );
	        $check_list_edit->specGroup = $specGroup;
	        $check_list_edit->goodsSpec = $goodsSpec;
        }

        $G_SHOP->smarty->assign_by_ref('editInfo', $check_list_edit);
        $G_SHOP->smarty->display('goods/goodsSkuSale.tpl');
    }  
    
     /**
     * 生产信息编辑
     *
     */
    public function goodsProductSale(){

        global $G_SHOP;
        
        //检验参数
        if (!isset($_REQUEST['postID'])) {
        	backendError();
        }
        
        //查询二级销售商品
        $check_list_edit = new entity('goods_list');
        $check_list_edit->postID = $_REQUEST['postID'];
        if (!$G_SHOP->DBCA->fetch($check_list_edit)) {
        	backendError('商品信息未找到');
        } else {
	        //城市
	        $check_list_edit->city = get_city();
        }
        
        //三级商品
        $quanliHandler = new quanliGoods( $G_SHOP->DBCA );
        $sale_array = array(
        	'twoPostID' => $_REQUEST['postID']
        );
        $specHandler = new goodsSpec( $G_SHOP->DBCA ) ;
        $saleGoods = $quanliHandler->searchGoodsSale( $sale_array );
        if ($saleGoods) {
        	foreach ($saleGoods as &$sale) {
        		//规格
        		$arg_array = array(
        			'postID' => $sale['postID']
        		);
        		$saleSpec_array = $specHandler->searchGoodsSpecRelation( $arg_array );
        		if ($saleSpec_array) {
        			$saleSpec_array = $saleSpec_array[0];
        			$sale['specGroupID'] = $saleSpec_array['specGroupID'];
        			$sale['specGroupName'] = $saleSpec_array['specGroupName'];
        			$sale['specID'] = $saleSpec_array['specID'];
        			$sale['specName'] = $saleSpec_array['specName'];
        		}
        		
        		//qs
        		$goodsHandler = new quanliGoods( $G_SHOP->DBCA );
        		$goodsqs_array = $goodsHandler->searchGoodsQs( $arg_array );
        		$goodsqs_array = $goodsqs_array[0];
        		if ($goodsqs_array) {
        			$sale['qsName'] = $goodsqs_array['qsName'];
        			$sale['recipe'] = $goodsqs_array['recipe'];
        			$sale['weight'] = $goodsqs_array['weight'];
        			$sale['noTaxCost'] = $goodsqs_array['noTaxCost'];
        			$sale['taxCost'] = $goodsqs_array['taxCost'];
        			$sale['checkPrice'] = $goodsqs_array['checkPrice'];
        		}
        		
        	}
        }
        $check_list_edit->saleGoods = $saleGoods;
        
        //请求查询参数
		$ary_array = array(
			'sortname' => 'sortOrder',
			'sortorder' => 'asc',
			'useFlg' => 1
		);
        
        //实例化分类类
        $categoryHandler = new goodsCategory( $G_SHOP->DBCA );
        $category = $categoryHandler->searchBigCategory( $ary_array );
        $check_list_edit->category = $category;

        //生产信息
        $produceHandler = new goodsProduce( $G_SHOP->DBCA );
        $product = $produceHandler->goodsProduceGroup( $ary_array );
        $check_list_edit->product = $product;
        
        //营养成分
        $nutrHandler = new goodsNutrition( $G_SHOP->DBCA );
        $nutrition = $nutrHandler->searchgoodsNutrition( $ary_array );
        $check_list_edit->nutrition = $nutrition;
        
        $G_SHOP->smarty->assign_by_ref('editInfo', $check_list_edit);
        
        //权限
        $G_SHOP->smarty->assign('cost_permission', $G_SHOP->has_permission(550));
        $G_SHOP->smarty->assign('detail_permission', $G_SHOP->has_permission(551));
        
        $G_SHOP->smarty->display('goods/goodsProductSale.tpl');
    } 
    
    /**
     * 商品图片
     *
     */
   	public function goodsPicSale(){
   		
   		global $G_SHOP;
		
		if (!isset($_REQUEST['postID'])) {
			backendError();
		}
		
		//二级商品
        $check_list_edit = new entity('goods_list_edit');
        $check_list_edit->postID = $_REQUEST['postID'];
        if (!$G_SHOP->DBCA->fetch($check_list_edit)) {
        	backendError('商品信息未找到');
        }
        
        //商品图片
        $commonHandler = new commonGoods( $G_SHOP->DBCA );
        $picture_array = array(
        	'postID' => $_REQUEST['postID'],
        	'sortname' => 'sortOrder',
        	'sortorder' => 'asc'
        );
        $goods_picture = $commonHandler->searchGoodsPicture( $picture_array );
        if ($goods_picture) {
        	foreach ($goods_picture as &$p) {
        		$p['p_big'] = str_replace('list', 'big', $p['pictureLink']);
        		$p['o_big'] = str_replace('list', 'big', $p['orgitalLink']);
        		$p['p_middle'] = str_replace('list', 'middle', $p['pictureLink']);
        		$p['o_middle'] = str_replace('list', 'middle', $p['orgitalLink']);
        		$p['p_small'] = str_replace('list', 'small', $p['pictureLink']);
        		$p['o_small'] = str_replace('list', 'small', $p['orgitalLink']);
        	}
        }
		$G_SHOP->smarty->assign_by_ref('goods_picture', $goods_picture);
		$G_SHOP->smarty->assign_by_ref('list', $check_list_edit);
   		
   		$G_SHOP->smarty->display('goods/goodsPicSale.tpl');
   	}

}